package com.jerry.controller;

import com.jerry.BO.ServerInfo;
import com.jerry.DTO.ServerInfoDTO;
import com.jerry.response.CommonResponse;
import com.jerry.service.DiscoveryService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/")
public class DiscoveryController {
    private final DiscoveryService discoveryService;

    public DiscoveryController(DiscoveryService discoveryService) {
        this.discoveryService = discoveryService;
    }

    @PostMapping("register")
    public CommonResponse regist(@RequestBody ServerInfoDTO serverInfo) {
        discoveryService.rigister(serverInfo);
        return CommonResponse.success();
    }

    @PutMapping("/heartbeat")
    public void heartbeat(@RequestBody ServerInfoDTO serverInfo) {
        discoveryService.heartbeat(serverInfo);
    }

    @GetMapping("/services")
    public CommonResponse services() {
        Map<String, List<ServerInfo>> map = discoveryService.services();
        return CommonResponse.success(map);
    }
}

